package com.villaday.idf.activity;

import android.content.Intent;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.adapter2.AddrAdapter;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.DividerItemDecoration.RVItemDecoration;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.model2.ShippingAddressesEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.ToolBarUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class AddrManagerActivity extends BaseActivity implements OkHttpUtil.OnDataListener, AbsRecyclerViewAdapter.OnItemClickListener, View.OnClickListener {

    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;
    @BindView(R.id.rv)
    RecyclerView rv;
    private LoadingDialog loadingDialog;
    private Gson gson = new GsonBuilder().create();
    private AddrAdapter addrAdapter;
    private ShippingAddressesEntity shippingAddressesEntity;
    private boolean needfinnish;


    @Override
    protected int getViewResId() {
        return R.layout.activity_addr_manager;
    }

    @Override
    protected void init() {
        ToolBarUtil.initToolBar(toolbarText, "收货地址", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.addItemDecoration(new RVItemDecoration(LinearLayout.VERTICAL, PhoneUtil.dp2px(this, 10), getResources().getColor(R.color.color2BG), PhoneUtil.dp2px(this, 10)));

        addrAdapter = new AddrAdapter(this,this);
        rv.setAdapter(addrAdapter);
        addrAdapter.setOnItemClickListener(this);
        addrAdapter.setEmptyView(R.layout.empty_tips);
        addrAdapter.setEmptyTips(R.id.tv_emptyTips, "");
        needfinnish = getIntent().getBooleanExtra("needfinnish", false);
    }

    @Override
    protected void loadData() {
        loadingDialog = LoadingDialog.newInstance("加载中...");
        loadingDialog.show(getFragmentManager());
        OkHttpUtil.getJSON(this, Constant.URL.shippingAddresses, this);
    }

    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            dismissLoading();
            LogeUtil.e("shippingAddresses", json);
            shippingAddressesEntity = gson.fromJson(json, ShippingAddressesEntity.class);

            if ("200".equals(shippingAddressesEntity.getCode())){
                addrAdapter.setData(shippingAddressesEntity.getBody());
            }

        }

    }

    @Override
    public void onFailure(String url, String error) {

    }

    @OnClick(R.id.tv_addnew)
    public void onViewClicked() {

        Intent intent = new Intent(this, NewAndEditAddrActivity.class);
        if (shippingAddressesEntity!=null&&shippingAddressesEntity.getBody()!=null&&shippingAddressesEntity.getBody().size()==0){
            intent.putExtra("isSelect",true);
        }
        startActivityForResult(intent, 123);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == 123) {
                loadData();
            }
        }
    }


    private void dismissLoading() {

        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }

    }

    @Override
    public void onItemClick(View v, int position) {
if (needfinnish){
    Intent entity = getIntent().putExtra("entity", shippingAddressesEntity.getBody().get(position));
    setResult(RESULT_OK,entity);
    finish();
}



    }

    @Override
    public void onClick(View v) {
            Intent intent = new Intent(this, NewAndEditAddrActivity.class);
        intent.putExtra("type",1);

        intent.putExtra("entity",shippingAddressesEntity.getBody().get((Integer) v.getTag()));
        startActivityForResult(intent, 123);
    }
}
